﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shorten.Core.Models;
using Shorten.Core.Data;
namespace Shorten.Core.Providers
{
    public class ShortenManagement
    {
        public static string GetLongUrlByShortUrl(string ShortUrl)
        {
            var data = new ShortenDataContext();

            return data.Urls.Where(f => f.ShortUrl == ShortUrl).Select(f => f.LongUrl).SingleOrDefault();
        }

        /// <summary>
        /// Returns the short URL when specifying a Long URL
        /// </summary>
        /// <param name="LongUrl"></param>
        /// <returns></returns>
        public static string GetShortUrlByLongUrl(string LongUrl)
        {
            var data = new ShortenDataContext();

            return data.Urls.Where(f => f.LongUrl == LongUrl).Select(f => f.ShortUrl).SingleOrDefault();
        }


        public static void AddShortenUrl(ShortenItems items)
        {
            var data = new ShortenDataContext();
            Url url = new Url();

            url.LongUrl = items.LongUrl;
            url.ShortUrl = items.ShortUrl;
            data.Urls.InsertOnSubmit(url);
            data.SubmitChanges();            
        }

        public static bool CheckIfShortenUrlExist(string ShortUrl)
        {
            var data = new ShortenDataContext();

            var q = from f in data.Urls
                    where f.ShortUrl == ShortUrl
                    select f.ShortUrl;

            if (q.Count() != 0)
            {
                return true;
            }

            return false;
           
        }

        public static string GetExistingShortenurl(string ShortUrl)
        {
            var data = new ShortenDataContext();
            var q = (from f in data.Urls
                     where f.ShortUrl == ShortUrl
                     select f.ShortUrl).SingleOrDefault();

            return q;
        }

        /// <summary>
        /// Check If the Long URL already exists
        /// </summary>
        /// <param name="LongUrl"></param>
        /// <returns></returns>
        public static bool CheckIfLongUrlExist(string LongUrl)
        {
            var data = new ShortenDataContext();

            var q = from f in data.Urls
                    where f.LongUrl == LongUrl
                    select f.ShortUrl;

            if (q.Count() != 0)
            {
                return true;
            }

            return false;
        }

        public static IList<ShortenItems> GetAllUrls()
        {
            var data = new ShortenDataContext();

            var q = from f in data.Urls
                    select new ShortenItems
                    {
                        LongUrl = f.LongUrl,
                        Id = f.Id,
                        ShortUrl = f.ShortUrl,
                    };

            return q.ToList();
        }

        /// <summary>
        /// Save Request is used as a loggin system or analytics integration
        /// </summary>
        /// <param name="ShortURL"></param>
        public static void SaveRequest(object AnalyticsObject)
        {
            //TODO - Implement analytics here
            //Need to define what is required to be stored and then store it.
            //Could we use Google Analytics here?
            //What was clicked
            //When it was clicked
            //Who clicked it
            //Where was it clicked
            //Why was it clicked - I do not think we can answer this
        }
    }
}
